/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessionbean;



import entity.StackUser;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Root;

/**
 *
 * @author wei
 */
@Stateless
public class UserFacade {

    @PersistenceContext(unitName = "CuteOverflow-warPU")
    private EntityManager entityManager;
    
    protected EntityManager getEntityManager() {
        return entityManager;
    }

    public void create(StackUser entity) {
        getEntityManager().persist(entity);
    }

    public void edit(StackUser entity) {
        getEntityManager().merge(entity);
    }

    public void remove(StackUser entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public StackUser find(Object id) {
        return (StackUser)getEntityManager().find(StackUser.class, id);
    }

   public StackUser getUserByEmail(String email) {
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<StackUser> dataObjectRoot = cq.from(StackUser.class);
        cq.select(dataObjectRoot);
        cq.where(builder.equal(dataObjectRoot.get("email"), email));

        return (StackUser) getEntityManager().createQuery(cq).getSingleResult();
    }   
   
}
